import uuid

from fastapi import APIRouter, HTTPException

from app.schemas.generate import (
    GenerateCommentsRequest, 
    GenerateCommentsWithNoDbRequest,
    GenerateCommentsToDbRequest
)
from app.services.textGenerationService import TextGenerationService
import logging
from app.crud.event import EventCRUD
from app.crud.generated_prompt import GeneratedPromptCRUD
from app.common.result import Result

router = APIRouter()

logging.basicConfig(level=logging.INFO)

# 创建服务实例
text_generation_service = TextGenerationService()

# @router.post("/generate")
# async def generate_text(prompt: str, number: int):
#     try:
#         result = TextGenerationService.generate_text(prompt=prompt, number=number)
#         if result['status'] == 'success':
#             return Result.success(data={"generated_texts": result['generated_texts']})
#         else:
#             return Result.fail(msg=result['message'])
#     except Exception as e:
#         return Result.fail(msg=f"系统错误: {str(e)}")

@router.post("/generate_comments")
async def generate_comments(request: GenerateCommentsRequest):
    try:
        # logging.info(f"Received data: event_id={event_id}, prompt_id={prompt_id}, number={number}, model_type={model_type}, last_editor={last_editor}")

        result = await text_generation_service.generate_comments(
            request.event_id, 
            request.prompt_id, 
            request.number, 
            request.model_type, 
            request.last_editor
        )
        if result['status'] == 'success':
            return Result.success(data={"generated_comments": result['generated_comments']})
        else:
            return Result.fail(msg=result['message'])
    except Exception as e:
        return Result.fail(msg=f"系统错误: {str(e)}")

@router.get("/get_comments/{event_id}")
async def get_comments(event_id: str):
    try:
        result = await text_generation_service.get_generated_comments_by_event(event_id)
        if result['status'] == 'success':
            return Result.success(data={"comments": result['comments']})
        else:
            return Result.fail(msg=result['message'])
    except Exception as e:
        return Result.fail(msg=f"系统错误: {str(e)}")

@router.get("/get_prompts/{event_id}")
async def get_prompts(event_id: str):
    try:
        result = await text_generation_service.get_generation_prompts_by_event(event_id)
        if result['status'] == 'success':
            return Result.success(data={"prompts": result['prompts']})
        else:
            return Result.fail(msg=result['message'])
    except Exception as e:
        return Result.fail(msg=f"系统错误: {str(e)}")

@router.get("/get_event/{event_id}")
async def get_event(event_id: str):
    try:
        result = await EventCRUD.get_event_by_id(event_id)
        if result:
            return Result.success(data={"event": result})
        else:
            return Result.fail(msg="Event not found")
    except Exception as e:
        return Result.fail(msg=f"系统错误: {str(e)}")

@router.post("/generate_comments_with_no_db")
async def generate_comments_with_no_db(request: GenerateCommentsWithNoDbRequest):
    try:
        result = await text_generation_service.generate_comments_with_no_db(request.prompt, request.number)
        if result['status'] == 'success':
            return Result.success(data={"generated_comments": result['generated_comments']})
        else:
            return Result.fail(msg=result['message'])
    except Exception as e:
        return Result.fail(msg=f"系统错误: {str(e)}")

@router.post("/generate_comments_to_db")
async def generate_comments_to_db(request: GenerateCommentsToDbRequest):
    try:
        if request.event_id == "":
            request.event_id = -1;
        if request.model_type == "":
            request.model_type = "Pro/deepseek-ai/DeepSeek-V3"
        if request.last_editor == "":
            request.last_editor = "admin"


        result = await text_generation_service.generate_comments_to_db(
            prompt=request.prompt,
            number=request.number,
            event_id=request.event_id,
            model_type=request.model_type,
            last_editor=request.last_editor
        )
        if result['status'] == 'success':
            return Result.success(data={"generated_comments": result['generated_comments']})
        else:
            return Result.fail(msg=result['message'])
    except Exception as e:
        return Result.fail(msg=f"系统错误: {str(e)}")